﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类PBrand。
    /// </summary>
    public class PBrandDAO
    {
        public PBrandDAO()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("BrandID", "PBrand");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int BrandID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@BrandID", SqlDbType.Int,4)};
            parameters[0].Value = BrandID;

            int result = DbHelperSQL.RunProcedure("UP_PBrand_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(DataEntity.PBrand model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@BrandID", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.VarChar,100),
					new SqlParameter("@Logo", SqlDbType.VarChar,255),
					new SqlParameter("@Description", SqlDbType.VarChar,3000),
					new SqlParameter("@Url", SqlDbType.VarChar,255),
					new SqlParameter("@SortOrder", SqlDbType.Int,4),
                    new SqlParameter("@CompanyName", SqlDbType.VarChar,300)};
            parameters[0].Value = model.BrandID;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Logo;
            parameters[3].Value = model.Description;
            parameters[4].Value = model.Url;
            parameters[5].Value = model.SortOrder;
            parameters[6].Value = model.CompanyName;
            DbHelperSQL.RunProcedure("UP_PBrand_ADD", parameters, out rowsAffected);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(DataEntity.PBrand model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@BrandID", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.VarChar,100),
					new SqlParameter("@Logo", SqlDbType.VarChar,255),
					new SqlParameter("@Description", SqlDbType.VarChar,3000),
					new SqlParameter("@Url", SqlDbType.VarChar,255),
					new SqlParameter("@SortOrder", SqlDbType.Int,4),
                    new SqlParameter("@CompanyName", SqlDbType.VarChar,300)};
            parameters[0].Value = model.BrandID;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Logo;
            parameters[3].Value = model.Description;
            parameters[4].Value = model.Url;
            parameters[5].Value = model.SortOrder;
            parameters[6].Value = model.CompanyName;
            DbHelperSQL.RunProcedure("UP_PBrand_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int BrandID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@BrandID", SqlDbType.Int,4)};
            parameters[0].Value = BrandID;

            DbHelperSQL.RunProcedure("UP_PBrand_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataEntity.PBrand GetModel(int BrandID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@BrandID", SqlDbType.Int,4)};
            parameters[0].Value = BrandID;

            DataEntity.PBrand model = new DataEntity.PBrand();
            DataSet ds = DbHelperSQL.RunProcedure("UP_PBrand_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["BrandID"].ToString() != "")
                {
                    model.BrandID = int.Parse(ds.Tables[0].Rows[0]["BrandID"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                model.Logo = ds.Tables[0].Rows[0]["Logo"].ToString();
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                model.Url = ds.Tables[0].Rows[0]["Url"].ToString();
                if (ds.Tables[0].Rows[0]["SortOrder"].ToString() != "")
                {
                    model.SortOrder = int.Parse(ds.Tables[0].Rows[0]["SortOrder"].ToString());
                }
                model.CompanyName = ds.Tables[0].Rows[0]["CompanyName"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BrandID,Name,Logo,Description,Url,SortOrder,CompanyName");
            strSql.Append(" FROM PBrand ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "PBrand";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}
